﻿using FineUI;
using SysManage.BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace SysManage.Module.Secretary
{
    public partial class PartySecretaryManageList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据加载
        private void BindData()
        {
            ControlBusiness.ControlBindDataDictionary(ddlCategory, "ZHDJ.Secretary.Category", true);
        }
        private void LoadData()
        {
            // 权限检查
            //CheckPowerWithButton("TalentWorldAdd", btnAdd);

            btnAdd.OnClientClick = Dialog.GetShowReference("PartySecretaryManageEdit.aspx", "新增数据");

            // 每页记录数 
            Grid1.PageSize = ConfigHelper.PageSize;

            BindGrid();
        }

        private void BindGrid()
        {
            IQueryable<PartySecretary> q = DB.PartySecretarys;

            string name = txtSecretaryName.Text.Trim();

            if (!string.IsNullOrEmpty(name))
            {
                q = q.Where(t => t.SecretaryName.Contains(name));
            }
            if (!string.IsNullOrEmpty(ddlCategory.SelectedValue))
            {
                q = q.Where(t => t.Category == ddlCategory.SelectedValue);
            }

            // 在查询添加之后，排序和分页之前获取总记录数
            Grid1.RecordCount = q.Count();

            // 排列和分页
            q = SortAndPage<PartySecretary>(q, Grid1);

            Grid1.DataSource = q;
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            //CheckPowerWithWindowField("TalentWorldEdit", Grid1, "editField");
            //CheckPowerWithLinkButtonField("TalentWorldDelete", Grid1, "deleteField");
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string id = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                //if (!CheckPower("TalentWorldDelete"))
                //{
                //    CheckPowerFailWithAlert();
                //    return;
                //}

                //var userCount = DB.PartyArticles.Count(t => t.ID == PartyArticleId);
                //if (userCount > 0)
                //{
                //    Alert.ShowInTop("删除失败！需要先清空作用此专题的题目！");
                //    return;
                //}

                var del = DB.PartySecretarys.FirstOrDefault(t => t.ID == id);
                DB.PartySecretarys.Remove(del);
                DB.SaveChanges();
                BindGrid();
            }
        }

        public string GetCategory(string category)
        {
            DataTable dt = PartyArticle.GetCategory("ZHDJ.Secretary.Category");
            if (dt.Rows.Count > 0)
            {
                DataRow dataRow = dt.Select("DDicMember='" + category + "'")[0];
                return Convert.ToString(dataRow["DDicMemberName"]);
            }
            else
            {
                return "";
            }
        }
        #endregion

        #region 按钮事件
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            //if (!CheckPower("TalentWorldDelete"))
            //{
            //    CheckPowerFailWithAlert();
            //    return;
            //}

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartySecretarys.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartySecretarys.Remove(u));
            DB.SaveChanges();

            // 重新绑定表格
            BindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
    }
}